package com.kangfawei.thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author 康发伟
 * 通过事项Runnable接口来创建线程
 */
public class DownloadURL2 implements Runnable{
    private String url;
    private String filename;
    private final File FILE_DIR = new File("E:/IO_target");

    public DownloadURL2(String url, String filename){
        this.url = url;
        this.filename = filename;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 5; i++) {
                FileUtils.copyURLToFile(new URL(this.url), new File(FILE_DIR, filename+i+".html"));
                System.out.println(filename+"--"+i);
                Thread.sleep(300);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        DownloadURL2 download1 = new DownloadURL2("https://www.baidu.com","baidu");
        DownloadURL2 download2 = new DownloadURL2("https://www.zhihu.com","zhihu");
        new Thread(download1).start();
        new Thread(download2).start();
    }
}
